package org.tgcloud.group.web.controller.manager;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.tgcloud.group.core.APPResultMessage;
import org.tgcloud.group.core.custom.Permissions;
import org.tgcloud.group.core.session.Session;
import org.tgcloud.group.entity.MjNormalUser;
import org.tgcloud.group.service.manager.NormalUserManageService;
import org.tgcloud.group.web.controller.dto.user.UserDTO;
import org.tgcloud.group.web.controller.req.user.ManageUserEditReq;
import org.tgcloud.group.web.controller.req.user.ManageUserAddReq;
import org.tgcloud.group.web.controller.req.user.ManageUserNameReq;
import org.tgcloud.group.web.controller.req.user.UserManageSearchReq;
import org.tgcloud.group.web.controller.req.user.UserTypeReq;

import java.util.Date;

/**
 * @author: zhy
 * @create: 2023-08-22 11:24
 * @description: 用户相关接口
 */
@Api(tags = "用户相关接口")
@RestController
@RequestMapping("/manager/pc/user")
@EnableAutoConfiguration
public class NormalUserManageController extends ManageAPPBaseController{

    @Autowired
    private NormalUserManageService userManageService;


    @Permissions("normalUser:view,order:push")
    @ApiOperation(value = "用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/userList", method = RequestMethod.POST)
    public APPResultMessage<Object, UserDTO> getUserList(@ModelAttribute("session") Session session,
                                                          @RequestBody UserManageSearchReq req) {
        return userManageService.getUserList(req.getPageNum(),req.getPageSize(),req.getKeyword(),req.getStartTime() == null ? null : new Date(req.getStartTime()),
                req.getEndTime() == null ? null : new Date(req.getEndTime()), req.getUserType(),req.getIsAssistant(), req.getId(),session.getComId());
    }


    @ApiOperation(value = "开拓者列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/selectPushList", method = RequestMethod.POST)
    public APPResultMessage<Object, MjNormalUser> selectPushList(@ModelAttribute("session") Session session,
                                                                 @RequestParam(required = false) Short isAssistant) {
        return userManageService.selectPushList(isAssistant == null ? (short)1 : isAssistant);
    }

    @ApiOperation(value = "助理列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/selectAssistantList", method = RequestMethod.POST)
    public APPResultMessage<Object, MjNormalUser> selectAssistantList(@ModelAttribute("session") Session session,
                                                                      @RequestParam(required = false) Integer assistantUserId) {
        return userManageService.selectAssistantList(assistantUserId);
    }



    @Permissions("normalUser:editUserType")
    @ApiOperation(value = "用户:修改用户类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/doUpdateUserType", method = RequestMethod.POST)
    public APPResultMessage doUpdateUserType(@ModelAttribute("session") Session session,
                                             @RequestBody UserTypeReq req) {
        return userManageService.doUpdateUserType(req);
    }


    @Permissions("normalUser:editUserName")
    @ApiOperation(value = "用户:修改用户名称")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/doUpdateUserName", method = RequestMethod.POST)
    public APPResultMessage doUpdateUserName(@ModelAttribute("session") Session session,
                                                           @RequestBody ManageUserNameReq req) {
        return userManageService.doUpdateUserName(req);
    }


    @Permissions("normalUser:add")
    @ApiOperation(value = "用户:新增用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/doAddUser", method = RequestMethod.POST)
    public APPResultMessage doAddUser(@ModelAttribute("session") Session session,
                                             @RequestBody ManageUserAddReq req) {
        return userManageService.doAddUser(req);
    }


    @Permissions("normalUser:edit")
    @ApiOperation(value = "用户:编辑用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/doEditUser", method = RequestMethod.POST)
    public APPResultMessage doEditUser(@ModelAttribute("session") Session session,
                                      @RequestBody ManageUserEditReq req) {
        return userManageService.doEditUser(req);
    }


    @ApiOperation(value = "用户:开拓者/助理级联关系")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token", required = true, dataType = "string"),
    })
    @ResponseBody
    @RequestMapping(value = "/getSelectionUserList", method = RequestMethod.POST)
    public APPResultMessage getSelectionUserList() {
        return userManageService.getSelectionUserList();
    }



}
